$(function(){
	/*获取文章信息*/
	var articleXq=JSON.parse(window.sessionStorage.getItem("articleXq"));
	var problemXq=JSON.parse(window.sessionStorage.getItem("problemXq"));
	console.log(articleXq)
	var userid = ym.getQueryStringByName('userid');
	var exchangeId = ym.getQueryStringByName('exchangeId');
	var curUrl= window.location.pathname;
	console.log(curUrl)
	var curType=curUrl.split('=')[1];
	console.log(articleXq,problemXq);
	var id = articleXq.exchange.exchangeId;
	var praiseType = 1;
   	var praiseTypeId = id;
	function isDZ(){
		var count=$('#dianzan').children()[0].children[0].children[1].innerText;
					var imgSrc=$('#dianzan').children()[0].childNodes[1].childNodes[0].firstChild.attributes[0].nodeValue;
					if(articleXq.praiseState===1){
						imgSrc='./../img/icon-active-dianzan.png'
						$('#dianzan').children()[0].childNodes[1].childNodes[0].firstChild.attributes[0].nodeValue=imgSrc;
					}else if(articleXq.praiseState===0){
						imgSrc='./../img/icon-dianzan.png'
						count--;
						$('#dianzan').children()[0].children[0].children[1].innerText=count;
						$('#dianzan').children()[0].childNodes[1].childNodes[0].firstChild.attributes[0].nodeValue=imgSrc;
					}
	}
	function getArticle(){
		isDZ();
		/*判断有无标题*/
		var title="";
		var title1="";
		var thoughCount="";
		var anserHTML="";
		var imgNavHTML="";
		var articleHTML="";
		var ohterAnswer="";
		for (var i = 0; i < articleXq.ymAnswers.length; i++) {
			if(articleXq.ymAnswers[i].parentId == 0||articleXq.ymAnswers[i].parentId==null){
          					anserHTML +='<div class="am-padding-0" parentId="'+articleXq.ymAnswers[i].parentId+'">'+
			          					'<div class="number xxpl font15 colorb6bcce">'+
			          					'<img src="'+articleXq.ymAnswers[i].headimgurl+'">'+
			          					'<div class="w70 am-margin-left-sm">'+
			          					'<span>'+articleXq.ymAnswers[i].userName+'</span>'+
			          					'<p class="color333 am-margin-top-0">'+articleXq.ymAnswers[i].answerDes+'</p>'+
			          					'<span class="font14">'+articleXq.ymAnswers[i].address+'<i class="am-margin-left-xs"></i></span>'+
			          					'</div>'+
			          					'<span class="plgr" id="'+articleXq.ymAnswers[i].answerDes+'"><img src="../img/pinglun.png"></span>'+
								        '</div>'; +
								        '<p style="display:none">'+
								        '<span class="usid">'+articleXq.ymAnswers[i].answerUserId+'</span>'+
								        '<span class="usna>'+articleXq.ymAnswers[i].userName+'</span>'+
								       	'</p>'+
								        '</div>';
          				}else{
          					anserHTML+= '<div class="am-padding-0" parentId="'+articleXq.ymAnswers[i].parentId+'">'+
			          					'<div class="number xxpl font15 colorb6bcce">'+
			          					'<img src="'+articleXq.ymAnswers[i].headimgurl+'">'+
			          					'<div class="w70 am-margin-left-sm">'+
			          					'<span>'+articleXq.ymAnswers[i].userName+'</span>'+
			          					'<p class="color333 am-margin-top-0"><span>回复<i class="color69e600 am-margin-left-xs">'+articleXq.ymAnswers[i].parentName+'</i>:</span>'+articleXq.ymAnswers[i].answerDes+'</p>'+
			          					'<span class="font14">'+articleXq.ymAnswers[i].address+'<i class="am-margin-left-xs"></i></span>'+
			          					'</div>'+
			          					'<span class="plgr"><img src="../img/pinglun.png"></span>'+
								        '</div>'+ 	
								        '<p style="display:none">'+
								        '<span class="usid">'+articleXq.ymAnswers[i].answerUserId+'</span>'+
								        '<span class="usna">'+articleXq.ymAnswers[i].userName+'</span>'+
								        '</p>'+
								        '</div>';
          				}
		}
		if(articleXq.ymAttachments.length!==0){
			for (var j = 0; j < articleXq.ymAttachments.length; j++) {
				imgNavHTML+="<li><img src='"+articleXq.ymAttachments[j].attachmentUrl+"' style='width:30%;height:7rem;margin:0.8rem'></li>"
			}
		}else{
			imgNavHTML="";
		}
		// articleHTML="<div class='problem-tab content-tab'>"+
		// 					  "<div class='person-tab-head'>"+
		// 					  		"<div class='head-image'><img src='"+ymAnswerVO.headimgurl+"' ></div>"+
		// 							"<div class='person-name'>"+
		// 								"<p class='person-name-text'>"+ymAnswerVO.nickName+"</p>"+
		// 							"</div>"+
		// 					  "</div>"+
		// 					  "<div class='problem-tab-desc'>"+
		// 					    "<p>"+ymAnswerVO.answerDes+"</p>"+
		// 					  "</div>"+
		// 					  "<ul class='imgList'>"+imgListHTML+"</ul>"+
		// 					  "<div class='problem-tab-foot'>"+
		// 					     "<div class='add-problem-time'><p>"+ymAnswerVO.createTimeString+"</p></div>"+
		// 					  	 "<div class='problem-dianzancount' id='dianzan' praiseState='"+ymAnswerVO.praiseState+"' answerId='"+ymAnswerVO.answerId+"'><p>"+zanHTML+"<sup> "+ymAnswerVO.praiseCountNumber+" </sup></p></div>"+
		// 					  	 "<div class='problem-pinluncount'><p><span><img src='./../img/icon-pinglun.png' ></span><sup> "+ymAnswerVO.answersCount+" </sup></p></div>"+
		// 					  "</div>"+
		// 					"</div>"+
		// 					"<div>"+pinlunHTML+
		// 					"</div>";
		$('.pinlun-tab').append(anserHTML);
		if(articleXq.title===undefined){
			title+="<a href='#title-link' class=''>帖子详情</a>";
			title+="";
			$('.am-header-title').append(title);
			$('.article-tab-title').append(title1);
		}else{
			title+="<a href='#title-link' class=''>"+articleXq.title+"</a>";
			title1+="<h2"+articleXq.title+"</h2>";
			$('.am-header-title').append(title);
			$('.article-tab-title').append(title1);
		}
		/*判断是否有浏览总数*/
		if(articleXq.thoughCount===undefined){
			thoughCount+="";
			$('#thoughtCount').append(thoughCount);
		}else{
			thoughCount+="<p><span><img src='./../img/eyes.png' alt=''></span><span> 4321 </span></p>";
			$('#thoughtCount').append(thoughCount);
		}
		
		$('#creatTime').html(articleXq.exchange.createTimeString);
		$('.article-tab-desc').html(articleXq.exchange.ecchangeMessage+imgNavHTML);
		$('#dzCount').html(articleXq.praise);
		$('#pinlunCount,.punlun-count-num').html(articleXq.answersCount);
		if(articleXq){

		}
	}
	getArticle();
	/*添加评论*/
	var urls = window.location.pathname+"?userid="+userid+"&exchangeId="+exchangeId;
	$('.am-navbar-nav #insert-pinlun').click(function(){
		$('textarea').focus();
		$('.pinlun-input textarea').attr('placeholder','添加评论：');
		$('.add-pinlun').css({'display':'block'});
		$('.navbar .am-navbar-nav').css({'display':'none'});
		$('.pinlun-subBtn').click(function(){
			ym.ajax({
		        url: "/communication/insertConsult",
		        param:{
		            answerDes:$('.pinlun-input textarea').val(),
		            typeId:articleXq.exchange.exchangeId,
		            answerUserId:1,
		        },
		        success: function (data) {
		        	ym.ajax({
				        url: "/ymUserInfrom/exchangeDetails",
				        param:{
				        	type:curType,
			            	exchangeId:articleXq.exchange.exchangeId
				        },
				        success: function (data) {
				        	if(data.obj){
								window.sessionStorage.setItem("articleXq",JSON.stringify(data.obj[0]));
								window.location.href='./../html/articleXq.html?type='+curType;
				        	}
				        }
				    });
		        }
		    });
		})
	})
	
	/*回复评论*/
	$('.plgr').click(function(){
		var curTab=$(this).parent().parent().children();
		var persoName=curTab[0].children[1].children[0].innerText;
		$('textarea').focus();
		$('.pinlun-input textarea').attr('placeholder','回复'+persoName+'：');
		$('.add-pinlun').css({'display':'block'});
		$('.navbar .am-navbar-nav').css({'display':'none'});
		var parentId=$(this).parent().parent().attr('parentId');
		$('.pinlun-subBtn').click(function(){
			ym.ajax({
		        url: "/communication/insertConsult",
		        param:{
		         	url:urls,
		            answerDes:$('.pinlun-input textarea').val(),
		            typeId:articleXq.exchange.exchangeId,
		            answerUserId:1,
		            parentId:parentId
		        },
		        success: function (data) {
		         	ym.ajax({
				        url: "/ymUserInfrom/exchangeDetails",
				        param:{
				        	type:curType,
			            	exchangeId:articleXq.exchange.exchangeId
				        },
				        success: function (data) {
				        	if(data.obj){
								window.sessionStorage.setItem("articleXq",JSON.stringify(data.obj[0]));
								window.location.href='./../html/articleXq.html?type='+curType;
				        	}
				        }
				    });
		        }
		    });
		})
	})
	/*点赞*/
   	$('#dianzan').on('click',function(){
   		
   		if(articleXq.praiseState == 1){
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					if(data.res ==1){
						var numbers = $('.dianz').text();
						if(data.obj=='0'){
							$("#shoucang").modal('open');
					   		var numbers = parseInt(numbers)-1;
					   		$('.dianz').text(numbers);
					   		$('#info-text').html('取消点赞');
					   		articleXq.praiseState = 0;
					   		return;
						}
					}else{
						$("#shoucang").modal('open');
			   			$('#info-text').html('取消点赞失败');
						setTimeout(function(){ $("#malert").modal('close');},800);
					}
				}
			});
   		}else if(articleXq.praiseState == 0){
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					if(data.res ==1){
						var numbers = $('.dianz').text();
						if(data.obj=='1'){
							$("#shoucang").modal('open');
					   		var numbers = parseInt(numbers)+1;
					   		$('.dianz').text(numbers);
					   		$('#info-text').html('点赞成功');
					   		articleXq.praiseState = 1;
					   		return;
						}
					}else{
						$("#shoucang").modal('open');
			   			$('#info-text').html('点赞失败');
						setTimeout(function(){ $("#malert").modal('close');},800);
					}
				}
			});
   		}else{
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					if(data.res ==1){
						var numbers = $('.dianz').text();
						if(data.obj=='获得豆豆+5'){
							$("#shoucang").modal('open');
					   		var numbers = parseInt(numbers)+1;
					   		$('.dianz').text(numbers);
					   		$('#info-text').html('点赞成功，豆豆+5');
					   		articleXq.praiseState = 1;
					   		return;
						}
					}else{
						$("#shoucang").modal('open');
			   			$('#info-text').html('点赞失败');
						setTimeout(function(){ $("#malert").modal('close');},800);
					}
				}
			});
   		}
   		
   })
   	function isCollect(){
   		/*判断是否已收藏*/
			ym.ajax({
				url:'/usercollectid/whetherCollection',
				param:{ 
					collectionTypeId :praiseTypeId,
				},
				success:function(data){
					if(data.obj ==1){
						$('.collect-icon').attr('src','../img/dynamic-active-star.png');
						$('#info-text').html('收藏成功');
					}else{
						$('.collect-icon').attr('src','../img/article-sc.png');
						
					}
				}
			});
   	}
   	isCollect();
   	/*收藏*/
   	$('#collect').on('click',function(){
   		ym.ajax({
			url:'/usercollectid/collection',
			param:{ 
				collectionType:praiseType,
				collectionTypeId :praiseTypeId,
			},
			success:function(data){
				isCollect();
			}
		});
   })
})